﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Sparkle.UI
{
    public class MetadataProvider : IMetadataProvider
    {
        /// <summary>
        /// Gets the metadata.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="pInfo">The p info.</param>
        /// <returns></returns>
        public Metadata GetMetadata(object entity, PropertyInfo pInfo)
        {
            Metadata metadata = new Metadata(pInfo);

            return metadata;
        }

        /// <summary>
        /// Gets the metadata.
        /// </summary>
        /// <param name="entity">The entity.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public Metadata GetMetadata(object entity, string propertyName)
        {
            if (entity == null)
                throw new NullReferenceException("entity can not be null.");
            PropertyInfo pInfo = entity.GetType().GetProperty(propertyName);
            if (pInfo == null)
                throw new NullReferenceException("propertyName not found.");

            return GetMetadata(entity, pInfo);
        }


        /// <summary>
        /// Gets the metadata.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public Metadata GetMetadata<T>(string propertyName)
        {
            PropertyInfo pinfo = typeof(T).GetProperty(propertyName);
            if (pinfo == null)
                throw new NullReferenceException("propertyName not found.");

            Metadata metadata = new Metadata(pinfo);

            return metadata;
        }


        /// <summary>
        /// GetMetadataCollection List
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static List<Metadata> GetMetadataCollection(object entity)
        {
            List<Metadata> metadataList = new List<Metadata>();

            foreach (PropertyInfo pInfo in entity.GetType().GetProperties())
            {
                metadataList.Add(UIDefaults.DefaultMetadataProvider.GetMetadata(entity, pInfo));
            }
            return metadataList;
        }

        /// <summary>
        /// GetMetadataCollection List
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public static List<Metadata> GetVisibleMetadataCollection(object entity)
        {
            List<Metadata> metadataList = GetMetadataCollection(entity);

            if (metadataList != null && metadataList.Count > 0)
            {
                var retMetadatas = from visibleMetadata in metadataList
                                   where visibleMetadata.Visible == true
                                   select visibleMetadata;
                return retMetadatas.ToList();
            }
            return metadataList;
        }


        /// <summary>
        /// GetMetadataCollection  Dictionary
        /// </summary>
        /// <param name="metadataList"></param>
        /// <returns></returns>
        public static Dictionary<string, Metadata> GetMetadataCollection(List<Metadata> metadataList)
        {
            Dictionary<string, Metadata> tmpMetadataList = new Dictionary<string, Metadata>();

            foreach (Metadata metadata in metadataList)
            {
                tmpMetadataList.Add(metadata.Name, metadata);
            }

            return tmpMetadataList;
        }



    }

}
